/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */

// 1. 实现树形结构, pid就是每个成员的父级
let data = [
    { id: 1, pid: 0 },
    { id: 2, pid: 1 },
    { id: 3, pid: 0 },
    { id: 4, pid: 2 },
];
let pid = [];
data.forEach((o) => pid.push(o.pid));
pid = pid.filter((v, i) => {
    return pid.indexOf(v) == i;
});
pid.forEach((v) => {
    data.forEach((o, i) => {
        let arr = []; //[1,3]
        let first,
            count = 0;
        if (o.pid == v && count == 0) first = o.id;
        if (o.pid == v && count != 0) {
            if (count == 1) arr.push(first);
            arr.push(o.id);
            o[id] = arr;
            count++;
        }
        if (v == o.id) {
            o.children = o.id;
        }
    });
});
console.log(data);
// 2. 不修改代码的情况下, 实现修改 obj 的内容
const test = (() => {
    let obj = {
        a: '变量a',
        b: '变量b',
    };
    function f(a) {
        obj['c'] = a;
    }
    return {
        get: (k) => {
            return obj[k];
        },
    };
})();
test.f('c');
console.log(test.get('c'));

// 3. 得出以下结果, 并且说明原因
window.number = 2;
let obj = {
    number: 3,
    db: (function () {
        this.number *= 4; //指向obj
        return function () {
            this.number *= 5; //指向obj
        };
    })(),
};
let db = obj.db;
db();
obj.db();
console.log(window.number); //2
console.log(obj.number); //3*5=15
